package com.example.thread.demo.sequence;

import org.junit.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 多线程顺序执行
 */
public class SequenceSolution {

    @Test
    public void test01() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("这是子线程");
            }
        });
        t1.start();
        t1.join();
        System.out.println("这是主线程");
//        t1.run();
    }

    /**
     * 多线程顺序执行
     */
    @Test
    public void test02() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1 point a");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t2 point b");
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t3 point c");
            }
        });

        t1.start();
        t2.start();
        t2.join();
        t3.start();
//        t3.join();
    }

    /**
     * 多线程顺序执行
     */
    @Test
    public void test03() {
        LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 1,
                TimeUnit.MINUTES, linkedBlockingQueue);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1 point a");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t2 point b");
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t3 point c");
            }
        });

        threadPoolExecutor.submit(t1);
        threadPoolExecutor.submit(t2);
        threadPoolExecutor.submit(t3);

        threadPoolExecutor.shutdown();

    }
}
